package com.java110.api.listener.fee;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.java110.api.listener.AbstractServiceApiPlusListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.dto.fee.FeeConfigDto;
import com.java110.intf.fee.IFeeConfigInnerServiceSMO;
import com.java110.intf.fee.IFeeInnerServiceSMO;
import com.java110.po.fee.PayFeePo;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.constant.lm.service.code.WeEnum;
import com.java110.utils.id.GeneratorUnionID;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@Java110Listener("weTopUpListener")
public class WeTopUpListener extends AbstractServiceApiPlusListener {

    @Autowired
    private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
    @Autowired
    private IFeeConfigInnerServiceSMO feeConfigInnerServiceSMOImpl;

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {

    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        String communityId = reqJson.getString("communityId");
        String roomId = reqJson.getString("roomId");
        String feeType = reqJson.getString("feeType");
        Double money = reqJson.getDouble("money");
        ResultVo vo = new ResultVo();
        if(Objects.isNull(money) || money <= 0){
            vo.setCode(801);
            vo.setMsg("金额有误");
        } else {
            FeeConfigDto dto = new FeeConfigDto();
            dto.setIsDefault("T");
            dto.setCommunityId(communityId);
            dto.setFeeTypeCd(feeType);
            List<FeeConfigDto> feeConfigDtos = feeConfigInnerServiceSMOImpl.queryFeeConfigs(dto);
            if(Objects.isNull(feeConfigDtos) || feeConfigDtos.size() != 1){
                vo.setCode(802);
                vo.setMsg("单价有误");
            } else {
                FeeConfigDto config = feeConfigDtos.get(0);
                try {
                    BigDecimal price = BigDecimal.valueOf(Double.valueOf(config.getSquarePrice()));
                    BigDecimal useLevel = BigDecimal.valueOf(money).divide(price,2,BigDecimal.ROUND_UP);
                    if(useLevel.doubleValue() > 0){
                        String id = GeneratorUnionID.valueOfString();
                        FeeConfigDto feeConfigDto = feeConfigDtos.get(0);
                        String start = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
                        String end = LocalDateTime.now().plusMinutes(5).format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
                        PayFeePo po = new PayFeePo();
                        po.setAmount(money.toString());
                        po.setCommunityId(communityId);
                        po.setConfigId(feeConfigDto.getConfigId());
                        po.setStartTime(start);
                        po.setEndTime(end);
                        po.setIncomeObjId("0");
                        po.setFeeTypeCd(feeType);
                        po.setFeeId(id);
                        po.setUserId(context.getRequestCurrentHeaders().get(CommonConstant.HTTP_USER_ID));
                        po.setPayerObjId(roomId);
                        po.setState("2008001");
                        po.setPayerObjType("3333");
                        po.setFeeFlag("2006012");
                        po.setPrice(price.toString());
                        po.setUseLevel(useLevel.toString());
                        int i = feeInnerServiceSMOImpl.saveFee(Lists.newArrayList(po));
                        if(i > 0){
                            vo.setCode(0);
                            vo.setMsg("ok");
                            vo.setData(id);
                        } else {
                            vo.setCode(501);
                            vo.setMsg("sql异常");
                        }
                    } else {
                        vo.setCode(802);
                        vo.setMsg("单价有误");
                    }
                } catch (Exception e){
                    vo.setCode(802);
                    vo.setMsg("单价有误");
                }
            }
        }
        ResponseEntity<String> responseEntity = new ResponseEntity(vo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);

    }

    @Override
    public String getServiceCode() {
        return WeEnum.we_top_up.getCode();
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }
}
